<?php

class RatsInputTest extends UnitTestCase {
    function TestRatsClientlib() {
        $this->UnitTestCase("Rats Input Tests");
    }

    // # we don't use the regular setUp and tearDown functions here, cause this would cause too much traffic
    // # instead we're relying on the fact that methods are executed in declaration order
    // function testSetup() {
    //   try {
    //     $rats = new Rats(SUPERUSER, SUPERPWD, BASE);
    //     $newUser = $rats->createUser(USER, EMAIL, PWD);
    //   }
    //   catch(Zend_Gdata_App_Exception $e) {
    //     assert(False);
    //   }
    // }
    
    function testCreateRassInput() {
      try {
        # create input profile (rass, no base uri)
        $rats = new Rats(USER, PWD, BASE);
        $input_entry = $rats->createInput(INPUT_RASS, "Me know nothing", METHOD_RASS, CDN01);
        $this->assertEqual($input_entry->content->params->name->text, INPUT_RASS);
        # retrieve the input profile
        $input2_entry = $rats->getInputEntry($input_entry->id->text);
        $this->assertEqual($input_entry->content->params->name->text, $input2_entry->content->params->name->text);
        # retrieve the input profile using the id as input
        $input3_entry = $rats->getInputEntryFromId($input_entry->content->params->id->text);
        $this->assertEqual($input2_entry->content->params->name->text, $input3_entry->content->params->name->text);
        # delete the input
        $rats->deleteInput($input_entry->id->text);
        $this->assertEqual($rats->getResponse()->getStatus(), 204);
      }
      catch(Zend_Gdata_App_Exception $e) {
        assert(False);
      }
    }

    function testCreateFtpInput() {
      try {
        # create input profile (rass, no base uri)
        $rats = new Rats(USER, PWD, BASE);
        $input_entry = $rats->createInput(INPUT_FTP, "I'm from Barcelona", METHOD_FTP, null, "ftp://myfileserver.glugle.org/fawlty/");
        $this->assertEqual($rats->getResponse()->getStatus(), 201);
        $this->assertEqual($input_entry->content->params->name->text, INPUT_FTP);
        # delete the input
        $rats->deleteInput($input_entry->id->text);
        $this->assertEqual($rats->getResponse()->getStatus(), 204);
      }
      catch(Zend_Gdata_App_Exception $e) {
        assert(False);
      }
    }
    
    function testCreateFtpInputNoBaseUri() {
      try {
        # create input profile (rass, no base uri)
        $rats = new Rats(USER, PWD, BASE);
        $input_entry = $rats->createInput(INPUT_FTP, null, METHOD_FTP, null);
        $this->assertEqual($rats->getResponse()->getStatus(), 201);
        $this->assertEqual($input_entry->content->params->name->text, INPUT_FTP);
        # delete the input
        $rats->deleteInput($input_entry->id->text);
        $this->assertEqual($rats->getResponse()->getStatus(), 204);
      }
      catch(Zend_Gdata_App_Exception $e) {
        assert(False);
      }
    }
    
    function testCheckCreatedParams() {
      try {
        # create input profile (rass, no base uri)
        $rats = new Rats(USER, PWD, BASE);
        $allow_overwrite = "1";
        $max_size = "300";
        $params = "<some>monster</some>";
        $input_entry = $rats->createInput(INPUT_FTP, DESCRIPTION, METHOD_FTP, null, BASE_URI, $max_size, $params);
        $this->assertEqual($rats->getResponse()->getStatus(), 201);
        $this->assertEqual($input_entry->content->params->name->text, INPUT_FTP);
        $this->assertEqual($input_entry->content->params->description->text, DESCRIPTION);
        $this->assertEqual($input_entry->content->params->method->text, METHOD_FTP);
        $this->assertEqual($input_entry->content->params->base_uri->text, BASE_URI);
        $this->assertEqual($input_entry->content->params->max_size->text, $max_size);
        $this->assertEqual($input_entry->content->params->params->text, $params);
        # retrieve the input profile using the id as input
        $input2_entry = $rats->getInputEntryFromId($input_entry->content->params->id->text);
        $this->assertEqual($input2_entry->content->params->name->text, INPUT_FTP);
        $this->assertEqual($input2_entry->content->params->description->text, DESCRIPTION);
        $this->assertEqual($input2_entry->content->params->method->text, METHOD_FTP);
        $this->assertEqual($input2_entry->content->params->base_uri->text, BASE_URI);
        $this->assertEqual($input2_entry->content->params->max_size->text, $max_size);
        $this->assertEqual($input2_entry->content->params->params->text, $params);
        
        # delete the input
        $rats->deleteInput($input_entry->id->text);
        $this->assertEqual($rats->getResponse()->getStatus(), 204);
      }
      catch(Zend_Gdata_App_Exception $e) {
        assert(False);
      }
    }
    
    function testCreateInputWithSameName() {
      $status = 0;
      try {
        # create input profile (rass, no base uri)
        $rats = new Rats(USER, PWD, BASE);
        $input_entry = $rats->createInput(INPUT_RASS, "Me know nothing", METHOD_RASS, CDN01);
        try {
          $input_entry = $rats->createInput(INPUT_RASS, null, METHOD_FTP, null);
          assert(False);
        }
        catch(Zend_Gdata_App_Exception $e) {
          $status = $e->getResponse()->getStatus();
        }
        $rats->deleteInput($input_entry->id->text);
      }
      catch(Zend_Gdata_App_Exception $e) {
        $status = $e->getResponse()->getStatus();
      }
      $this->assertEqual($status, 400); 
    }
    
    function testCreateInvalidInputNoMethod() {
      $status = 0;
      try {
        # create input profile (rass, no base uri)
        $rats = new Rats(USER, PWD, BASE);
        # no method, so should not be accepted
        $input_entry = $rats->createInput(INPUT_FTP, "Me know nothing", null, CDN01, "ftp://myfileserver.glugle.org/fawlty/");
        assert(False);
      }
      catch(Zend_Gdata_App_Exception $e) {
        $status = $e->getResponse()->getStatus();
      }
      $this->assertEqual($status, 400); 
    }
    
    function testCreateInvalidMethod() {
      $status = 0;
      try {
        # create input profile (rass, no base uri)
        $rats = new Rats(USER, PWD, BASE);
        # no method, so should not be accepted
        $input_entry = $rats->createInput(INPUT_FTP, "Me know nothing", "gaga", null, "ftp://myfileserver.glugle.org/fawlty/");
        assert(False);
      }
      catch(Zend_Gdata_App_Exception $e) {
        $status = $e->getResponse()->getStatus();
      }
      $this->assertEqual($status, 400); 
    }
    
    
    function testCreateInvalidInputNoName() {
      $status = 0;
      try {
        # create input profile (rass, no base uri)
        $rats = new Rats(USER, PWD, BASE);
        # no method, so should not be accepted
        $input_entry = $rats->createInput(null, "Me know nothing", METHOD_RASS, CDN01, "ftp://myfileserver.glugle.org/fawlty/");
        assert(False);
      }
      catch(Zend_Gdata_App_Exception $e) {
        $status = $e->getResponse()->getStatus();
      }
      $this->assertEqual($status, 400); 
    }
    
    function testCreateInvalidInputRassNoCDN() {
      $status = 0;
      try {
        # create input profile (rass, no base uri)
        $rats = new Rats(USER, PWD, BASE);
        # no method, so should not be accepted
        $input_entry = $rats->createInput(INPUT_RASS, "Me know nothing", METHOD_RASS, null, "ftp://myfileserver.glugle.org/fawlty/");
        assert(False);
      }
      catch(Zend_Gdata_App_Exception $e) {
        $status = $e->getResponse()->getStatus();
      }
      $this->assertEqual($status, 400); 
    }
    
    function testCreateInvalidInputFtpWrongBaseUri() {
      $status = 0;
      try {
        # create input profile (rass, no base uri)
        $rats = new Rats(USER, PWD, BASE);
        # no method, so should not be accepted
        $input_entry = $rats->createInput(INPUT_FTP, "Me know nothing", METHOD_FTP, null, "hi/this_is_nice");
        assert(False);
      }
      catch(Zend_Gdata_App_Exception $e) {
        $status = $e->getResponse()->getStatus();
      }
      $this->assertEqual($status, 400); 
    }
    
    function testCreateInvalidInputRassWrongBaseUri() {
      $status = 0;
      try {
        # create input profile (rass, no base uri)
        $rats = new Rats(USER, PWD, BASE);
        # no method, so should not be accepted
        $input_entry = $rats->createInput(INPUT_RASS, "Me know nothing", METHOD_RASS, CDN01, "http://doom.com/");
        assert(False);
      }
      catch(Zend_Gdata_App_Exception $e) {
        $status = $e->getResponse()->getStatus();
      }
      $this->assertEqual($status, 400); 
    }

    
    function testCreateRassUnicodeInput() {
      try {
        # create input profile (rass, no base uri)
        $rats = new Rats(USER, PWD, BASE);
        $name = "Père François est tres méchant !";
        $description = "wh@t = m+$$ ?";
        $input_entry = $rats->createInput($name, $description, METHOD_RASS, CDN01);
        $this->assertEqual($input_entry->content->params->name->text, $name);
        $this->assertEqual($input_entry->content->params->description->text, $description);
        # retrieve the input profile
        $input2_entry = $rats->getInputEntry($input_entry->id->text);
        $this->assertEqual($input_entry->content->params->name->text, $input2_entry->content->params->name->text);
        # retrieve the input profile using the id as input
        $input3_entry = $rats->getInputEntryFromId($input_entry->content->params->id->text);
        $this->assertEqual($input2_entry->content->params->name->text, $input3_entry->content->params->name->text);
        # delete the input
        $rats->deleteInput($input_entry->id->text);
        $this->assertEqual($rats->getResponse()->getStatus(), 204);
      }
      catch(Zend_Gdata_App_Exception $e) {
        assert(False);
      }
    }

    // 
    // function testTeardown() {
    //   try {
    //     $rats = new Rats(SUPERUSER, SUPERPWD, BASE);
    //     $newUser = $rats->deleteUser(USER);
    //   }
    //   catch(Zend_Gdata_App_Exception $e) {
    //     assert(False);
    //   }
    // }
}
